home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
168_01
/
scn.c
< prev
next >
Wrap
Text File
|
1985-08-19
|
15KB
|
637 lines
/* SDB - token scanning routines */
#include "stdio.h"
#include "sdbio.h"
int dbv_token; /* current token */
int dbv_tvalue; /* integer token value */
char dbv_tstring[STRINGMAX+1]; /* string token value */
struct ifile *dbv_ifp; /* indirect file context */
struct macro *dbv_macros; /* macro definitions */
int dbv_fold; /* case fold alpha comparisons */
static char *iprompt,*cprompt; /* input prompts */
static char cmdline[LINEMAX+2],*lptr; /* current line and pointer */
static int atbol; /* flag indicating at bol */
static int savech; /* lookahead character */
static int savetkn; /* lookahead token */
static char *keywords[] = { /* keyword table */
"ascending",
"by",
"char",
"compress",
"create",
"define",
"delete",
"descending",
"exit",
"export",
"extract",
"from",
"help",
"insert",
"import",
"into",
"num",
"print",
"select",
"set",
"show",
"sort",
"update",
"using",
"where",
NULL
};
static int keytokens[] = { /* token values for each keyword */
ASCENDING,
BY,
CHAR,
COMPRESS,
CREATE,
DEFINE,
DELETE,
DESCENDING,
EXIT,
EXPORT,
EXTRACT,
FROM,
HELP,
INSERT,
IMPORT,
INTO,
NUM,
PRINT,
SELECT,
SET,
SHOW,
SORT,
UPDATE,
USING,
WHERE,
NULL
};
/* db_sinit - initialize the scanner */
db_sinit()
{
/* at beginning of line */
atbol = TRUE;
/* make the command line null */
lptr = NULL;
/* no lookahead yet */
savech = EOS;
savetkn = NULL;
/* no indirect command files */
dbv_ifp = NULL;
/* no macros defined */
dbv_macros = NULL;
/* fold alpha comparisons */
dbv_fold = TRUE;
}
/* db_prompt(ip,cp) - initialize prompt strings */
db_prompt(ip,cp)
char *ip,*cp;
{
/* save initial and continuation prompt strings */
iprompt = ip;
cprompt = cp;
}
/* db_scan(fmt,args) - initiate line scan command parsing */
db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)
{
/* convert the command line and arguments */
sprintf(cmdline,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10);
/* start at the beginning of the command line */
lptr = cmdline;
iprompt = NULL;
dbv_ifp = NULL;
/* no lookahead yet */
savech = EOS;
savetkn = NULL;
/* fold alpha comparisons */
dbv_fold = TRUE;
}
/* db_flush - flush the current input line */
int db_flush()
{
while (savech != '\n')
if (savech > ' ')
return (db_ferror(SYNTAX));
else
savech = getchx();
savech = EOS;
atbol = TRUE;
return (TRUE);
}
/* db_gline - get a line from the current input */
char *db_gline(buf)
char *buf;
{
int ch,i;
for (i = 0; (ch = getch()) != '\n' && ch != -1; )
if (i < LINEMAX)
buf[i++] = ch;
else {
printf("*** line too long ***\nRetype> ");
i = 0;
}
buf[i] = EOS;
return (buf);
}
/* db_ifile - setup an indirect command file */
int db_ifile(fname)
char *fname;
{
struct ifile *new_ifp;
if ((new_ifp = malloc(sizeof(struct ifile))) == NULL)
return (db_ferror(INSMEM));
else if ((new_ifp->if_fp = fopen(fname,"r")) == NULL) {
free(new_ifp);
return (db_ferror(INDFNF));
}
new_ifp->if_mtext = NULL;
new_ifp->if_savech = savech;
new_ifp->if_lptr = lptr;
new_ifp->if_next = dbv_ifp;
dbv_ifp = new_ifp;
/* return successfully */
return (TRUE);
}
/* db_kill - kill indirect command file input */
db_kill()
{
struct ifile *old_ifp;
while ((old_ifp = dbv_ifp) != NULL) {
dbv_ifp = old_ifp->if_next;
if (old_ifp->if_fp != NULL)
fclose(old_ifp->if_fp);
savech = old_ifp->if_savech;
lptr = old_ifp->if_lptr;
free(old_ifp);
}
while (savech != '\n')
savech = getchx();
savech = EOS;
savetkn = NULL;
atbol = TRUE;
}
/* db_token - return the current input token */
int db_token()
{
struct macro *mptr;
struct ifile *new_ifp;
/* find a token that's not a macro call */
while (db_xtoken() == ID) {
/* check for a macro call */
for (mptr = dbv_macros; mptr != NULL; mptr = mptr->mc_next)
if (db_scmp(dbv_tstring,mptr->mc_name) == 0) {
if ((new_ifp = malloc(sizeof(struct ifile))) == NULL)
printf("*** error expanding macro: %s ***\n",dbv_tstring);
else {
new_ifp->if_fp = NULL;
new_ifp->if_mtext = mptr->mc_mtext->mt_next;
new_ifp->if_lptr = lptr; lptr = mptr->mc_mtext->mt_text;
new_ifp->if_savech = savech; savech = EOS;
new_ifp->if_next = dbv_ifp;
dbv_ifp = new_ifp;
}
savetkn = NULL;
break;
}
if (mptr == NULL)
break;
}
return (dbv_token);
}
/* db_xtoken - return the current input token */
int db_xtoken()
{
int ch;
/* check for a saved token */
if ((dbv_token = savetkn) != NULL)
return (dbv_token);
/* get the next non-blank character */
ch = nextch();
/* check type of character */
if (isalpha(ch)) /* identifier or keyword */
get_id();
else if (isdigit(ch)) /* number */
get_number();
else if (ch == '"') /* string */
get_string();
else if (get_rel()) /* relational operator */
;
else /* single character token */
dbv_token = getch();
/* save the lookahead token */
savetkn = dbv_token;
/* return the token */
return (dbv_token);
}
/* db_ntoken - get next token (after skipping the current one) */
int db_ntoken()
{
/* get the current token */
db_token();
/* make sure another is read on next call */
savetkn = NULL;
/* return the current token */
return (dbv_token);
}
/* db_xntoken - get next token (after skipping the current one) */
int db_xntoken()
{
/* get the current token */
db_xtoken();
/* make sure another is read on next call */
savetkn = NULL;
/* return the current token */
return (dbv_token);
}
/* db_scmp - compare two strings */
int db_scmp(str1,str2)
char *str1,*str2;
{
if (dbv_fold)
return (scmp(str1,str2));
else
return (strcmp(str1,str2));
}
/* db_sncmp - compare two strings with a maximum length */
int db_sncmp(str1,str2,len)
char *str1,*str2; int len;
{
if (dbv_fold)
return (sncmp(str1,str2,len));
else
return (strncmp(str1,str2,len));
}
/* scmp - compare two strings with alpha case folding */
static int scmp(str1,str2)
char *str1,*str2;
{
int ch1,ch2;
/* compare each character */
while (*str1 && *str2) {
/* fold the character from the first string */
if (isupper(*str1))
ch1 = tolower(*str1++);
else
ch1 = *str1++;
/* fold the character from the second string */
if (isupper(*str2))
ch2 = tolower(*str2++);
else
ch2 = *str2++;
/* compare the characters */
if (ch1 != ch2)
if (ch1 < ch2)
return (-1);
else
return (1);
}
/* check for strings of different lengths */
if (*str1 == *str2)
return (0);
else if (*str1 == 0)
return (-1);
else
return (1);
}
/* sncmp - compare two